package com.google.code.swingonfire.utility.date;

import java.awt.geom.Rectangle2D;
import java.util.Calendar;

class DateRendererYearPart extends AbstractDateRenderPart
{

    private static final String[] SHORT_VALUES = {"00", "11", "22", "33", "44", "55", "66", "77", "88", "99"};
    private static final String[] LONG_VALUES = {"0000", "1111", "2222", "3333", "4444", "5555", "6666", "7777",
        "8888", "9999"};

    public DateRendererYearPart(DateRenderer info)
    {
        super(info);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Rectangle2D computeMaximumBounds(DateRendererContext context, DateFormatStyle style)
    {
        String[] values = (style == DateFormatStyle.SHORT) ? SHORT_VALUES : LONG_VALUES;

        return findMaximumBounds(context, values);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getValue(DateRendererContext context, DateFormatStyle style, Calendar calendar)
    {
        String year = String.valueOf(calendar.get(Calendar.YEAR));

        if ((style == DateFormatStyle.SHORT) && (year.length() > 2))
        {
            return year.substring(year.length() - 2);
        }

        return year;
    }

}
